package com.taihua_tech.basic.modul1.controller;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.json.simple.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.taihua_tech.basic.modul1.dao.HaoBoDao;
import com.taihua_tech.basic.util.AttachmentPathUtil;
import com.taihua_tech.basic.util.DataParam;
import com.taihua_tech.basic.util.DataRow;
 

@Controller
@RequestMapping("/modul1")
public class HaoBoController {
	@Resource//注入
	private HaoBoDao haoBoDao;
	
	@RequestMapping("/haobo.html")
	public String initPage(ModelMap model,@RequestParam Map<String,String> map){
		System.out.println("进入了 HaoBoController 控制器方法");
		List<DataRow>list=haoBoDao.findList(new DataParam());
		System.out.println(list);
		model.addAttribute("renlist", list);
		return "/modul1/HaoBoPage";
	}
	
	@PostMapping ("/haobo.do")
	@ResponseBody
	public List<DataRow> findList(@RequestBody DataParam param){
		System.out.println("param333:"+param);
		int i=1/0;  //这里异常，aop会触发，记录日志
		List<DataRow>list=haoBoDao.findList(new DataParam());
		return list;
	}
	 
	@PostMapping(value = "/{id}/bo.do", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public String addPhoto(@PathVariable int id, HttpServletRequest request,RedirectAttributes redirectAttributes,
				@RequestParam("file_param") MultipartFile file) throws Exception {
		FileOutputStream fos = new FileOutputStream(request.getServletContext().getRealPath("/") + file.getOriginalFilename());
		IOUtils.copy(file.getInputStream(), fos);
		fos.close();
		//可用将重定向的参数传递出去
		redirectAttributes.addAttribute("message", "文件已上传:"+file.getOriginalFilename());
		return  "redirect:/modul1/haobo.html";
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping(value = "/kindEditor.do", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	@ResponseBody
	public String upLoadFile(HttpServletRequest request, @RequestParam("imgFile") MultipartFile file) throws Exception {
		String path =AttachmentPathUtil.getAttachmentUploadPath(request);
		FileOutputStream fos = new FileOutputStream(path +file.getOriginalFilename());
		IOUtils.copy(file.getInputStream(), fos);
		fos.close();
		String netPath =AttachmentPathUtil.getAttachmentNetPath(request);
		String url=netPath+file.getOriginalFilename();
		System.out.println("#######"+url+"#########");
		
		JSONObject jsObj=new JSONObject();
		jsObj.put("error", 0);
		jsObj.put("url",url);
		jsObj.put("title", file.getOriginalFilename());
		return jsObj.toJSONString() ;
	}
}




